package com.cynen.p42u.service.impl;

import com.cynen.p42u.dao.JavDictDao;
import com.cynen.p42u.dao.JavLibDao;
import com.cynen.p42u.pojo.JavDict;
import com.cynen.p42u.pojo.JavLibInfo;
import com.cynen.p42u.service.JavLibService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class JavLibServiceImpl implements JavLibService {

    @Autowired
    private JavLibDao javLibDao;

    @Autowired
    private JavDictDao javDictDao;

    @Override
    public void save(JavLibInfo javLibInfo) {
        // 保存影片主要信息
        javLibDao.save(javLibInfo);
    }

    @Override
    public List findAll(JavLibInfo javLibInfo) {

        Example<JavLibInfo> example = Example.of(javLibInfo);
        List<JavLibInfo> all = javLibDao.findAll(example);
        return all;
    }

    @Override
    public List findAl() {
        return javLibDao.findAll();
    }

    @Override
    public void update(JavLibInfo javLibInfo) {
        // 直接根据ID更新.
        Optional<JavLibInfo> javLibInfo1 = javLibDao.findById(javLibInfo.getId());
        if (javLibInfo1.isPresent()){
            javLibDao.save(javLibInfo);
        }

/*        Optional<JavLibInfo> javLibInfo2 = javLibDao.findById(javLibInfo.getId());
        if (javLibInfo2.isPresent()){
            // 更新下载次数.
            javLibInfo2.get().setDownloads(javLibInfo2.get().getDownloads()+1);
            javLibDao.saveAndFlush(javLibInfo2.get());
        }*/
    }

    // 分页查询
    @Override
    public Page findPage(int start,int pageSize){
        PageRequest pageable = PageRequest.of(start,pageSize);
        return  javLibDao.findAll(pageable);
    }

}
